<template>
    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body @close="close">
        <el-form ref="form" :model="form" :rules="rules" label-width="150px">
            <el-form-item label="驱动名称" prop="driverName">
                <el-input v-model="form.driverName" placeholder="请输入驱动名称"/>
            </el-form-item>
            <el-form-item v-if="!form.driverId" label="协议类型" prop="driverType">
                <el-select v-model="form.driverType">
                    <el-option
                            v-for="dict in driverTypeOptions"
                            :key="dict.dictValue"
                            :label="dict.dictLabel"
                            :value="dict.dictValue"
                    ></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import {addDriver, updateDriver} from "/src/api/driver/manage";

    export default {
        name: "update",
        emits: ['dialogStatusChanged'],
        props: {
            open: Boolean,
            title: String,
            form: Object,
            driverTypeOptions:Array,
        },
        data() {
            return {
                // 表单校验
                rules: {
                    driverName: [
                        {required: true, message: "驱动名称不能为空", trigger: "blur"}
                    ],
                    driverType: [
                        {required: true, message: "驱动类型不能为空", trigger: "blur"}
                    ],
                }
            }
        },
        methods: {
            //提交
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        if (this.form.driverId != undefined) {
                            updateDriver(this.form).then(response => {
                                this.msgSuccess("修改成功");
                                this.close();
                            });
                        } else {
                            addDriver(this.form).then(response => {
                                this.msgSuccess("新增成功");
                                this.close();
                            });
                        }
                    }
                })
            },
            //取消
            cancel() {
                this.$emit('dialogStatusChanged', false)
            },
            //关闭
            close() {
                this.$emit('dialogStatusChanged', false)
            }
        }
    }
</script>

<style scoped>

</style>
